package com.stockz.scanner.screen;

import java.util.List;

import com.stockz.core.model.Bar;
import com.stockz.core.util.BarUtil;
import com.stockz.core.util.BarUtil.BarValueType;
import com.stockz.scanner.screen.configuration.BullishBarScreenConfiguration;

public class BullishBarScreen implements Screen<Object> {

	private BullishBarScreenConfiguration configuration
		= new BullishBarScreenConfiguration();
	
	@Override
	public Object getConfiguration() {
		return configuration;
	}

	@Override
	public int getMandatorySpan() {
		return 3;
	}

	@Override
	public boolean isMatch(List<Bar> barList) {
		if(barList == null || barList.size() <= getMandatorySpan()){
			return false;
		}
		
		Bar current = barList.get(0);
		Bar previous = barList.get(1);
		
		if(configuration.isCheckClose()){
			if(current.getClose() < (current.getLow() + ((current.getHigh()-current.getLow())
					*configuration.getCloseLowerPercentangeInSpread()/100))){
				return false;
			}
		}
		
		if(configuration.isCheckAvgPrice()){
			if(BarUtil.getValue(current, BarValueType.AVGPRICE) < (current.getLow() + ((current.getHigh()-current.getLow())
					*configuration.getAvgPriceLowerPercentageInSpread()/100))){
				return false;
			}
		}
		
		if(configuration.isCheckAvgPriceHigherThanPrevious()
				&& BarUtil.getValue(current, BarValueType.AVGPRICE) < BarUtil.getValue(previous, BarValueType.AVGPRICE)){
			return false;
		}
		if(configuration.isCheckCloseHigherThanPrevious()
				&& current.getClose() < previous.getClose()){
			return false;
		}
		if(configuration.isCheckHighLowHigherThanPrevious()
				&& (current.getHigh() < previous.getHigh() || current.getLow() < previous.getLow())){
			return false;
		}
		if(configuration.isCheckVolumeHigherThanPrevious()
				&& current.getVolume() < previous.getVolume()){
			return false;
		}
		if(configuration.isCheckSpreadHigherThanPrevious()
				&& BarUtil.getValue(current, BarValueType.SPREAD) < BarUtil.getValue(previous, BarValueType.SPREAD)){
			return false;
		}
		return true;
	}

	public void setConfiguration(BullishBarScreenConfiguration configuration) {
		this.configuration = configuration;
	}

}